package pers.qianyu.month_202101.date_20210121;

import java.util.*;

/**
 * 155. 最小栈
 * https://leetcode-cn.com/problems/min-stack/
 *
 * @author mizzle rain
 * @date 2021年1月21日15:36:57
 */
public class MinStack {
    private Deque<Integer> st = new LinkedList<>();
    private Deque<Integer> minStack = new LinkedList<>();

    /**
     * initialize your data structure here.
     */
    public MinStack() {

    }

    public void push(int x) {
        st.addLast(x);
        if (minStack.isEmpty() || x < minStack.getLast()) {
            minStack.addLast(x);
        } else {
            minStack.addLast(minStack.getLast());
        }
    }

    public void pop() {
        st.pollLast();
        minStack.pollLast();
    }

    public int top() {
        return st.getLast();
    }

    public int getMin() {
        return minStack.getLast();
    }

    public static void main(String[] args) {
        MinStack st = new MinStack();
        st.push(-2);
        st.push(0);
        st.push(-3);
        System.out.println(st.getMin());
        st.pop();
        System.out.println(st.top());
        System.out.println(st.getMin());
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */